import com.editgrid.api.client.*;
import java.util.*;

public class SpreadsheetSample {

	public static void main(String[] args) {
		EditGridService service = new EditGridService("__AppId__");
		try {
			service.authenticate("__Login__", "__Password__");
			User me = service.getAuthenticatedUser();
			
			// Listing spreadsheets in my workspace
			List<Spreadsheet> ssList = service.listSpreadsheet(me.getResourcePath());
			System.out.println("Spreadsheets in my workspace:");
			for (Iterator<Spreadsheet> i = ssList.iterator(); i.hasNext();) {
				Spreadsheet spreadsheet = i.next();
				System.out.println(spreadsheet.getSpreadsheetName());
			}
			
			// Creating a spreadsheet
			Spreadsheet spreadsheet = service.createSpreadsheet(me.getResourcePath(), "New spreadsheet", "");
			System.out.println("Spreadsheet created: " + spreadsheet.getSpreadsheetName() + ".");
			
			// Inserting a sheet
			spreadsheet.createSheet("New Sheet", 0);
			System.out.println("Insert sheet: New Sheet.");
			
			// Setting cell A1 in New Sheet
			spreadsheet.setCellValue("New Sheet", new CellPosition(1,1), "=1+1");
			System.out.println("Update cell A1 to =1+1.");
			
			// Getting cell values in New Sheet
			List<Cell> cellList = spreadsheet.getCellValueInRange("New Sheet", new CellRange(1,1, 10, 10));
			System.out.println("Cell value in New Sheet:");
			for (Iterator<Cell> i = cellList.iterator();i.hasNext();) {
				Cell cell = i.next();
				System.out.println(cell.getRow() + "," + cell.getCol() + ": " + cell.getFormattedValue());
			}
			
			// Delete a spreadsheet
			service.deleteSpreadsheet(spreadsheet.getResourcePath());
			System.out.println("Spreadsheet deleted.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
